﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCT.Huiteng.Utility.Config;

namespace DCT.Huiteng.Utility.Log
{
    public abstract class LogSkeleton : ILog
    {
        public abstract void D(string tag, string msg, Exception exception);
        public abstract void I(string tag, string msg, Exception exception);
        public abstract void W(string tag, string msg, Exception exception);
        public abstract void E(string tag, string msg, Exception exception);
        public abstract void F(string tag, string msg, Exception exception);


        /// <summary>
        /// 日志级别，如果是空字符串或all，就所有级别都记录；如果需要过滤，则Debug,Info,Warning,Error,Fatal，用逗号隔开
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public virtual bool EnabledLog(LogLevel level)
        {
            return EnabledLog(level, this.ConfigParser);
        }

        protected bool EnabledLog(LogLevel level, IConfigParser config)
        {
            string levelString = level.ToString().ToLower();
            string levelInConfig = config.GetVal("level", "").ToLower();

            if (levelInConfig == ""
                || levelInConfig == "all"
                || levelInConfig.Split(',').Contains(level.ToString().ToLower()))
            {
                return true;
            }
            return false;
        }
        
        public IConfigParser ConfigParser { get; protected set; }

        public virtual IConfigParser LoadConfig(IConfigParser config)
        {
            this.ConfigParser = config;
            return this.ConfigParser;
        }
        public virtual void D(string tag, string msg)
        {
            this.D(tag, msg, null);
        }

        public virtual void I(string tag, string msg)
        {
            this.I(tag, msg, null);
        }

        public virtual void W(string tag, string msg)
        {
            this.W(tag, msg, null);
        }

        public virtual void E(string tag, string msg)
        {
            this.E(tag, msg, null);
        }

        public virtual void F(string tag, string msg)
        {
            this.F(tag, msg, null);
        }

        public virtual void Log(LogLevel level, string tag, string msg)
        {
            this.Log(level, tag, msg, null);
        }

        public virtual void Log(LogLevel level, string tag, string msg, Exception exception)
        {
            switch (level)
            {
                case LogLevel.Debug:
                    {
                        this.D(tag, msg, exception);
                        break;
                    }
                case LogLevel.Info:
                    {
                        this.I(tag, msg, exception);
                        break;
                    }
                case LogLevel.Warning:
                    {
                        this.W(tag, msg, exception);
                        break;
                    }
                case LogLevel.Error:
                    {
                        this.E(tag, msg, exception);
                        break;
                    }
                case LogLevel.Fatal:
                    {
                        this.F(tag, msg, exception);
                        break;
                    }
            }
        }

        public virtual void Log(LogData logData)
        {
            this.Log(logData.Level, logData.Tag, logData.Msg, logData.Exception);
        }
    }
}
